package logic

import (
	"context"
	"fmt"
	"mini/model/redis"
	"time"

	"mini/userrpc/internal/svc"
	"mini/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
	return &SendSmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SendSmsLogic) SendSms(in *userrpc.SendSmsRequest) (*userrpc.SendSmsResponse, error) {
	// todo: add your logic here and delete this line
	/*i := rand.Intn(9000) + 1000
	resp, err := pkg.SendSms(in.Tel, strconv.Itoa(i))
	if err != nil {
		return nil, fmt.Errorf("短信验证码获取失败")
	}
	if *resp.Body.Code != "OK" {
		return nil, fmt.Errorf(*resp.Body.Message)
	}*/

	NumLoc := "sendSmsNum" + in.Source + in.Tel
	get := redis.Rdb.Get(NumLoc)
	if get.Val() != "" {
		return nil, fmt.Errorf("1分钟只能发送一次短信")
	}
	redis.Rdb.Set("sendSms"+in.Source+in.Tel, "1111", time.Minute*5)
	redis.Rdb.Set(NumLoc, "1", time.Minute*1)

	return &userrpc.SendSmsResponse{}, nil
}
